package constraint;

import java.util.ArrayList;
import java.util.List;

import common.MCore;

import choco.Constraint;
import expression.MIntVar;


public class MAnd extends MConstraint {
	private List<MConstraint> constraints;

	public MAnd(MCore c) {
		super(c);
		constraints = new ArrayList<MConstraint>();
	}
	
	public List<MConstraint> getConstraints() {
		return constraints;
	}

	public Constraint constraint() {
		Constraint[] csts = new Constraint[constraints.size()];
		for (int i = 0; i < csts.length; i++) {
			csts[i] = constraints.get(i).eval().constraint();
		}
		
		return getCore().getProblem().and(csts);
	}

	public MConstraint eval() {
		MAnd cst = new MAnd(getCore());
		for (MConstraint c: constraints) {
			MConstraint temp = c.eval();
			if (temp instanceof MFalse) {
				return temp;
			}
			else {
				cst.getConstraints().add(temp);
			}
		}
		return cst;
	}

	public List<MIntVar> getVariables() {
		List<MIntVar> vars = new ArrayList<MIntVar>();
		
		for (MConstraint c: constraints) {
			for (MIntVar v: c.getVariables()) {
				vars.add(v);
			}
		}
		
		return vars;
	}

	public String toText() {
		String s = "(and";
		
		for (MConstraint c: constraints) {
			s += " " + c;
		}
		s += ")";
		
		return s;
	}
}
